from urllib import request
import re
import os

url = "https://www.mzitu.com/xinggan/"

headers = {
    "referer": "https://www.mzitu.com/167645"
}
req = request.Request(url=url,headers=headers)
# 发送请求
response = request.urlopen(req)

# 返回数据
html = response.read().decode('utf-8')
# 创建路径
targetPath = os.getcwd() + os.path.sep + 'b' + os.path.sep
# 判断路径存不存在
if not os.path.exists(targetPath):
    # 创建文件夹
    os.makedirs(targetPath)
# print(html)

# 定义规则
pat = re.compile(r'<dd><a .*?>(.*?)</a></dd>')
res = pat.findall(html)
print(res)
# # 下载图片
# for item in res:
#     targetPath2 = (targetPath+item.split("/")[-1])
#
#     print(item)
#     request.urlretrieve(item,targetPath2)
